// index.js
var app = getApp();
var bmap = require('../../../common/bmap-wx.min.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: app.appimg,
    index: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    cityIndex: 0,
    text: '',
    windowwidth: '',
    scrolllength: 0,
    show: false,
    isShow: true,
    city: "定位中..."
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.showLoadings();
    var that = this;
    this.data.windowwidth = wx.getSystemInfoSync().windowWidth;
    // 城市列表
    this.city();
    var tmpcity = options.city;
    if (tmpcity == null || tmpcity == "" || typeof(tmpcity) == undefined) {
      tmpcity = "";
    }
    // 获取用户当前城市及用户当前城市的班次信息
    this.position(tmpcity);
  },

  position: function (tmpcity) {
    let that = this;
    if (tmpcity !== ""){
      this.setData({ city: tmpcity });
      that.first(tmpcity);
      return
    }
    // 百度地图定位获取当前位置信息
    var BMap = new bmap.BMapWX({
      ak: 'D097238894b7c9f3ddf8fd92be560188'
    });
    var success = function(data) {
      let city = data.originalData.result.addressComponent.city;
      // 班次信息
      that.first(city);
      that.setData({
        city: data.originalData.result.addressComponent.city,
      });
    }
    var fail = function(data) {}
    BMap.regeocoding({
      fail: fail,
      success: success
    });
  },

  // 初始请求
  first: function(city) {
    var that = this;
    wx.request({
      url: app.serviceurl + 'CarRental/CarRentalQueryLines',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        StartCity: city
      },
      dataType: 'json',
      success: function(res) {
        if (!res.data.Result) {
          that.setData({
            data: res.data,
          });
          return
        };
        that.setData({
          noneCity: false,
          data: res.data,
          isShow: true
        });
      },
      fail: function(res) {
        console.log(res)
      },
      complete: function(res) {
        app.hideLoadings();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.removeStorage({
      key: 'getSite',
      success: function(res) {}
    });
    this.setData({
      text: app.notice,
      // IsRegister: app.IsRegister
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },

  city: function() {
    var that = this;
    wx.request({
      url: app.serviceurl + 'CarRental/CarRentalQueryCities',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {},
      dataType: 'json',
      success: function(res) {
        if (!res.data.Result) {
          that.setData({
            cityShow: res.data,
          });
          return
        };
        var citys = [];
        if (res.data.City != null && typeof(res.data.City) != undefined) {
          res.data.City.forEach(function(e) {
            citys.push(e.CityName);
          });
        }
        that.setData({
          citys: citys,
          cityShow: res.data,
          cityHidens: true
        });
      },
      fail: function(res) {
        console.log(res)
      }
    })
  },

  bindCountryChange: function(e) {
    app.showLoadings();
    var that = this;
    this.setData({
      cityIndex: e.detail.value,
      show: true,
    });

    var city = this.data.citys[e.detail.value];
    wx.request({
      url: app.serviceurl + 'CarRental/CarRentalQueryLines',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        StartCity: city
      },
      dataType: 'json',
      success: function(res) {
        if (!res.data.Result) {
          app.hideLoadings();
          that.setData({
            data: res.data,
            noneCity: true
          });
          return;
        };
        that.setData({
          data: res.data,
          noneCity: false
        });

        app.hideLoadings();
      },
      fail: function(res) {
        console.log(res)
        app.hideLoadings();
      }
    })
  },
  order: function(e) {
    if (app.IsRegister == true) {
      var lineid = e.currentTarget.dataset.lineid;
      var linename = e.currentTarget.dataset.linename,
        pathid = e.currentTarget.dataset.pathid,
        bag = e.currentTarget.dataset.bag,
        packprice = e.currentTarget.dataset.packprice,
        description = e.currentTarget.dataset.description,
        EndCity = e.currentTarget.dataset.endcity,
        spellprice = e.currentTarget.dataset.spellprice;
      wx.setStorage({
        key: "EndCity",
        data: EndCity
      });
      wx: wx.navigateTo({
        url: '../order/order?lineid=' + lineid + "&linename=" + linename +
          '&pathid=' + pathid + '&bag=' + bag + '&packprice=' + packprice + '&spellprice=' + spellprice + '&description=' + description,
      })
    } else {
      wx.showModal({
        content: '请绑定手机号后再来购票哦！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../../../bindPhone/bindPhone',
            });
          }
        }
      });
    }
  }
})